﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Text;

namespace Gollie.Graph
{
	public class LabelValues
	{
		public GraphAxisLabelsPosition position { get; set;}
		public StringCollection labels { get; set; }
	}

	public class GraphAxisLabels :  Collection<LabelValues>// Dictionary<GraphAxisLabelsPosition, StringCollection>
	{
		internal string Url
		{
			get
			{
				if (this.Count == 0)
					return "";

				StringBuilder chxt = new StringBuilder("chxt=");
				StringBuilder chxl = new StringBuilder("chxl=", 128);
				StringBuilder chxs = new StringBuilder("chxs=", 128);
				int cnt = 0;

				foreach (LabelValues lv in this)
				{
					if (cnt > 0)
					{
						chxt.Append(",");
						chxs.Append("|");
					}

					chxs.Append(cnt);
					chxs.Append(",");

					switch (lv.position)
					{
						case GraphAxisLabelsPosition.Bottom:
							chxt.Append("x");
							break;
						case GraphAxisLabelsPosition.Left:
							chxt.Append("y");
							break;
						case GraphAxisLabelsPosition.Right:
							chxt.Append("r");
							break;
						case GraphAxisLabelsPosition.Top:
							chxt.Append("t");
							break;
					}

					StringCollection labels = lv.labels;
					if (labels.Count > 0)
					{
						chxl.Append(cnt.ToString());
						chxl.Append(":|");

						chxs.Append(Graph.StringColor(System.Drawing.Color.Black));
						if (lv.position == GraphAxisLabelsPosition.Bottom)
							chxs.Append(",8"); //font size
						else
							chxs.Append(",12"); //font size

						foreach (string label in labels)
						{
							chxl.Append(HttpUtility.UrlEncode(label));
							chxl.Append("|");
						}

					}

					cnt++;
				}
				if (chxl[chxl.Length - 1] == '|')
					chxl.Remove(chxl.Length - 1, 1); //remove final '|'

				StringBuilder url = new StringBuilder(chxt.ToString(), chxt.Length + chxl.Length + 8);
				url.Append("&");
				url.Append(chxl.ToString());

				url.Append("&");
				url.Append(chxs.ToString());

				return url.ToString();
			}
		}
	}
}